package com.zn.task;

import com.alibaba.fastjson.JSONObject;
import com.zn.service.DocumentaryTradeService;
import com.zn.service.OptionTradeService;
import com.zn.util.Constant;
import com.zn.util.DateUtils;
import com.zn.vo.OptionSymbol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author YinJiaWei
 * @version 2019/1/21 16:11
 * 模拟交易
 */
@Component
public class MockTradeTask {
    private static final Logger LOGGER = LoggerFactory.getLogger(MockTradeTask.class);
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 模拟开仓报单回报
     * @param parameter reqorder|客户交易账号|母账户|合约code|本地报单编号|价格|手数|多空方向|开平方向|止损点位
     */
    public void mockReportOrder(OptionTradeService optionTradeService,String parameter){
        try {

            String [] parameters = parameter.split("\\|");
            String tradeDay= DateUtils.getDateTime("yyyyMMdd", new Date());
            String tradeTime= DateUtils.getDateTime("HH:mm:ss", new Date());
            String instrumentCode=parameters[3],orderRef=parameters[4],price=parameters[5],volume=parameters[6],direction=parameters[7],offsetFlag=parameters[8],hedging=parameters[10];
            /**获取最新价**/
            String today= DateUtils.convertDateToString(new Date());
            String optionKey= Constant.OPTION_SYMBOL_INFO + today;
            LOGGER.info("optionKey------->"+optionKey);
            Object optionSymbol=redisTemplate.opsForHash().get(optionKey, instrumentCode);
            String lastPrice="";
            if(optionSymbol!=null){
                OptionSymbol optionSymbol2Json= JSONObject.parseObject(optionSymbol.toString(), OptionSymbol.class);
                String tickKey= Constant.OPTION_TICKL_INFO+optionSymbol2Json.getTargetCode()+":"+optionSymbol2Json.getInstrumentMonth()+":"+today;
                Object optionTick=redisTemplate.opsForHash().get(tickKey, instrumentCode);
                JSONObject optionMarketTickDataField=JSONObject.parseObject(optionTick.toString());
                lastPrice=optionMarketTickDataField.get("lastPrice").toString();
            }else{
                lastPrice=price;
            }

            if("1".equals(hedging)){
                /**模拟第一条报单已提交响应**/
                /**report|交易日|报单提交状态|报单状态|报单编号|报单来源|报单类型|数量|最小成交量|今成交数量|报单价格|本地报单编号|合约|状态信息|报单时间|错误ID**/
                String respReportOne="hedgingReport|"+tradeDay+"|3|3||0|0|"+volume+"|"+volume+"|0|"+lastPrice+"|"+orderRef+"|"+instrumentCode+"|报单已提交"+"|"+tradeTime+"|0|0|0|0|";
                optionTradeService.respHedgingReportOrder(respReportOne);
                Thread.sleep(500);
                /**时间戳模拟ctp返回报单编号**/
                long orderSysId=System.currentTimeMillis();
                /**模拟第二条报单全部成交报单已提交**/
                String respReportTwo="hedgingReport|"+tradeDay+"|3|0|"+orderSysId+"|0|0|"+volume+"|"+volume+"|"+volume+"|"+lastPrice+"|"+orderRef+"|"+instrumentCode+"|全部成交报单已提交"+"|"+tradeTime+"|0|0|0|0|";
                optionTradeService.respHedgingReportOrder(respReportTwo);
                Thread.sleep(500);
                long tradeId=System.currentTimeMillis();
                /**模拟第三条成交回报**/
                /**hedging|开平方向|本地报单编号|合约code|报单编号|成交编号|成交日期|成交时间|手数|价格|买卖类型|交易类型**/
                String respDeal="hedgingDeal|"+offsetFlag+"|"+orderRef+"|"+instrumentCode+"|"+orderSysId+"|"+tradeId+"|"+tradeDay+"|"+tradeTime+"|"+volume+"|"+lastPrice+"|"+direction+"|0";
                optionTradeService.respHedgingDealOrder(respDeal);
            }else if("0".equals(hedging)){
                /**模拟第一条报单已提交响应**/
                /**report|交易日|报单提交状态|报单状态|报单编号|报单来源|报单类型|数量|最小成交量|今成交数量|报单价格|本地报单编号|合约|状态信息|报单时间|错误ID**/
                String respReportOne="report|"+tradeDay+"|3|3||0|0|"+volume+"|"+volume+"|0|"+lastPrice+"|"+orderRef+"|"+instrumentCode+"|报单已提交"+"|"+tradeTime+"|0|0|0|0|";
                optionTradeService.respReportOrder(respReportOne);
                Thread.sleep(500);
                /**时间戳模拟ctp返回报单编号**/
                long orderSysId=System.currentTimeMillis();
                /**模拟第二条报单全部成交报单已提交**/
                String respReportTwo="report|"+tradeDay+"|3|0|"+orderSysId+"|0|0|"+volume+"|"+volume+"|"+volume+"|"+lastPrice+"|"+orderRef+"|"+instrumentCode+"|全部成交报单已提交"+"|"+tradeTime+"|0|0|0|0|";
                optionTradeService.respReportOrder(respReportTwo);
                Thread.sleep(500);
                long tradeId=System.currentTimeMillis();
                String respDeal="deal|"+offsetFlag+"|"+orderRef+"|"+instrumentCode+"|"+orderSysId+"|"+tradeId+"|"+tradeDay+"|"+tradeTime+"|"+volume+"|"+lastPrice+"|"+direction+"|0";
                optionTradeService.respDealOrder(respDeal);
            }
        }catch (Exception e){
            LOGGER.error("mockOpenReportOrder Error------->",e);
        }
    }

    public void mockDocuReportOrder(DocumentaryTradeService documentaryTradeService,String itme){
        try {
            String [] parameters = itme.split("\\|");
            String tradeDay= DateUtils.getDateTime("yyyyMMdd", new Date());
            String tradeTime= DateUtils.getDateTime("HH:mm:ss", new Date());
            //reqorder|dev10001001|100001002|10001670|1|0.0395|5|1|1|dev190228100045|2|0
            //reqorder|dev10001001|100001002|10001670|dev190228100051|1|0.0395|5|1|1|dev190228100049|2|0
            String instrumentCode=parameters[3],orderRef=parameters[4],price=parameters[5],volume=parameters[6],direction=parameters[7],offsetFlag=parameters[8],hedging=parameters[10];
            /**获取最新价**/
            String today= DateUtils.convertDateToString(new Date());
            String optionKey= Constant.OPTION_SYMBOL_INFO + today;
            LOGGER.info("optionKey------->"+optionKey);
            Object optionSymbol=redisTemplate.opsForHash().get(optionKey, instrumentCode);
            String lastPrice="";
            if(optionSymbol!=null){
                OptionSymbol optionSymbol2Json= JSONObject.parseObject(optionSymbol.toString(), OptionSymbol.class);
                String tickKey= Constant.OPTION_TICKL_INFO+optionSymbol2Json.getTargetCode()+":"+optionSymbol2Json.getInstrumentMonth()+":"+today;
                Object optionTick=redisTemplate.opsForHash().get(tickKey, instrumentCode);
                JSONObject optionMarketTickDataField=JSONObject.parseObject(optionTick.toString());
                lastPrice=optionMarketTickDataField.get("lastPrice").toString();
            }else{
                lastPrice=price;
            }
            if("2".equals(hedging)){
                /**模拟第一条报单已提交响应**/
                /**report|交易日|报单提交状态|报单状态|报单编号|报单来源|报单类型|数量|最小成交量|今成交数量|报单价格|本地报单编号|合约|状态信息|报单时间|错误ID**/
                String respReportOne="docuReport|"+tradeDay+"|3|3||0|0|"+volume+"|"+volume+"|0|"+lastPrice+"|"+orderRef+"|"+instrumentCode+"|报单已提交"+"|"+tradeTime+"|0|0|0|0|";
                documentaryTradeService.respDocumentaryReportOrder(respReportOne);
                Thread.sleep(500);
                /**时间戳模拟ctp返回报单编号**/
                long orderSysId=System.currentTimeMillis();
                /**模拟第二条报单全部成交报单已提交**/
                String respReportTwo="docuReport|"+tradeDay+"|3|0|"+orderSysId+"|0|0|"+volume+"|"+volume+"|"+volume+"|"+lastPrice+"|"+orderRef+"|"+instrumentCode+"|全部成交报单已提交"+"|"+tradeTime+"|0|0|0|0|";
                documentaryTradeService.respDocumentaryReportOrder(respReportTwo);
                Thread.sleep(500);
                long tradeId=System.currentTimeMillis();
                String respDeal="docuDeal|"+offsetFlag+"|"+orderRef+"|"+instrumentCode+"|"+orderSysId+"|"+tradeId+"|"+tradeDay+"|"+tradeTime+"|"+volume+"|"+lastPrice+"|"+direction+"|0";
                documentaryTradeService.respDocumentaryDealOrder(respDeal);
            }
        }catch (Exception e){
            LOGGER.error("mockDocuReportOrder Error------->",e);
        }

    }
}
